package by.demchenko.sample.webapp.page.admin;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.enums.ScheduleStatus;
import by.demchenko.sample.services.CourseService;
import by.demchenko.sample.services.PersonService;
import by.demchenko.sample.services.ScheduleService;
import by.demchenko.sample.services.SectionService;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class ScheduleEditPage extends BaseAdminPage {

    @Inject
    ScheduleService scheduleService;

    @Inject
    PersonService personService;

    @Inject
    SectionService sectionService;

    @Inject
    CourseService courseService;

    public ScheduleEditPage(Schedule schedule) {
        super();

        add(new Label("headerTitle", getString("label.schedule")));
        add(new Label("subtitle", getString("label.editor")));
        add(new FeedbackPanel("feedback"));

        Form<Schedule> form = new Form<>("form", new CompoundPropertyModel<>(schedule));

        List<Course> courseList = courseService.getAll();
        final DropDownChoice<Course> tfCourse = new DropDownChoice<>(
                "course",
                new PropertyModel<>(schedule, "course"),
                courseList,
                new ChoiceRenderer<>("name", "id"));
        tfCourse.setLabel(new ResourceModel("schedule.course"));
        tfCourse.setRequired(true);
        form.add(tfCourse);

        final TextField<Date> tfStartDate = new TextField<Date>("startDate");
        tfStartDate.add(new PropertyValidator<>());
        form.add(tfStartDate);

        final TextField<Date> tfEndDate = new TextField<Date>("endDate");
        tfEndDate.add(new PropertyValidator<>());
        form.add(tfEndDate);

        final TextField<Integer> tfTotal = new TextField<>("total");
        tfTotal.add(new PropertyValidator<>());
        form.add(tfTotal);

        final TextField<Integer> tfRegistered = new TextField<>("registered");
        tfRegistered.add(new PropertyValidator<>());
        form.add(tfRegistered);

        final DropDownChoice<ScheduleStatus> tfStatus = new DropDownChoice<>(
                "status",
                new PropertyModel<>(schedule, "status"),
                Arrays.asList(ScheduleStatus.values()),
                new EnumChoiceRenderer<>(this));
        tfStatus.add(new PropertyValidator<>());
        tfStatus.setLabel(new ResourceModel("schedule.status"));
        form.add(tfStatus);


        form.add(new SubmitLink("sumbit-link") {
            @Override
            public void onSubmit() {
                super.onSubmit();
                scheduleService.saveOrUpdate(schedule);
                SchedulePage page = new SchedulePage();
                setResponsePage(page);
            }

            @Override
            public void onError() {
                super.onError();
            }
        });

        form.add(new Link<Void>("back-link") {

            @Override
            public void onClick() {
                setResponsePage(new SchedulePage());
            }
        });

        add(form);
    }
}
